package by.dvrudenko.avtobaza.webapp.page;

import java.sql.Timestamp;
import java.util.Calendar;

import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.ResourceModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import by.dvrudenko.avtobaza.datamodel.Car;
import by.dvrudenko.avtobaza.datamodel.Driver;
import by.dvrudenko.avtobaza.datamodel.User;
import by.dvrudenko.avtobaza.datamodel.enums.OrderStatus;
import by.dvrudenko.avtobaza.webapp.app.BasicAuthenticationSession;

public class AbstractPanel extends Panel {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(AbstractPanel.class);	
	final protected User user = BasicAuthenticationSession.get().getUser();
	final protected static String DEFAULT_DRIVER_IMAGE = "defaultDriver.jpg";
	public final static String DRIVER_FOTO_PATH="D:/images/driver/";
	final protected static String  IMAGE_SERVICE = "http://localhost:8081/imagesService/"; 
	
	
	
	final private Calendar calendar = Calendar.getInstance();
	final private int currentYear = calendar.get(Calendar.YEAR);

	public AbstractPanel(String id) {
		super(id);
	}

	@Override
	protected void onInitialize() {
		super.onInitialize();
	}

	// сколько прошло лет от переданной даты
	public int getYearFromDate(Timestamp time) {
		calendar.setTimeInMillis(time.getTime());
		int date = calendar.get(Calendar.YEAR);
		return currentYear - date;
	}

	// возвращает год
	protected int getYear(Timestamp time) {
		calendar.setTimeInMillis(time.getTime());
		return calendar.get(Calendar.YEAR);
	}

	protected IModel<String> getResourceModel(String str) {
		return new ResourceModel(str);
	}

	protected Model<String> handleValueCarCondition(Car car) {
		if (car.getCarCondition().equals(true)) {
			return Model.of("рабочая");
		} else {
			return Model.of("поломаная");
		}
	}
	
	protected Model<String> handleValueCarWork(Car car) {
		if (car.getWork().equals(true)) {
			return Model.of("уехала");
		} else {
			return Model.of("в гараже");
		}
	}
	
	protected Model<Integer> handleYear(Timestamp temestamp){
		return Model.of(getYear(temestamp));
	}
	
	protected Model<Integer> handleYearFromCurrent(Timestamp temestamp){
		return Model.of(getYearFromDate(temestamp));
	}

	
	protected Model<String> handleValueDriverWork(Driver driver) {
		if (driver.getWork().equals(true)) {
			return Model.of("уехал");
		} else {
			return Model.of("свободен");
		}
	}
	
	
	protected String getImageDriver(Driver driver) {
		final String urlStr;
		if (driver.getFoto()==null || driver.getFoto()==""){
			 urlStr=IMAGE_SERVICE + DEFAULT_DRIVER_IMAGE;
		}else{
			 urlStr=IMAGE_SERVICE+driver.getFoto();
		}
		return urlStr;
	}
	
	

}
